import collections
class Solution:
    def minCost(self, basket1, basket2) -> int:
        c1 = collections.Counter(basket1)
        c2 = collections.Counter(basket2)
        c = c1 + c2
        for key in c:
            if c[key] % 2:
                return -1
            else:
                c[key] = c[key] // 2
        t = 0
        for key in c:
            t += abs(c1[key] - c[key])
        ans = 0
        t //= 2
        now_add = 0
        key_list = list(c.keys())
        key_list.sort()
        min_val = min(c.keys())
        for key in key_list:
            if key < 2 * min_val:
                now = key
            else:
                now = 2 * min_val
            if now_add + abs(c[key] - c1[key]) <= t:
                ans += now * abs(c[key] - c1[key])
                now_add += abs(c[key] - c1[key])
            else:
                ans += now * (t - now_add)
                break
        return ans

data = Solution()
basket1 = [84,80,43,8,80,88,43,14,100,88]
basket2 = [32,32,42,68,68,100,42,84,14,8]
print(data.minCost(basket1, basket2))